﻿
function getCurrentOpinion() {
    var opinion = new Object();
    opinion.ReservationId = parseInt($('input[name*=ReservationId]').val());
    opinion.ClientFirstName = $('input[name*=FirstName]').val();
    opinion.ClientLastName = $('input[name*=LastName]').val();
    opinion.Country = $('input[name$=txtCountry_value]').val();
    opinion.StayType = $('select[name=stayType]').val();
    opinion.InPlus = $('textarea[name=inplus]').val();
    opinion.InMinus = $('textarea[name=inminus]').val();
    opinion.PartOpinions = $.map($('.add-opn-cat'), function (n, i) {
        return {
            categoryId: parseInt($(n).attr('catid')),
            value: parseFloat($(n).find('.aoc-v').text())
        }
    });

    return opinion;
}

function beforeSaveOpinion() {
    $('.ui-dialog-buttonpane').html('<div class="aoc-sv"><img src="/Data/SiteImages/loader.gif" /><div class="ts">' + language.saving + '</div></div>');
}

function saveComplete() {
    $('.add-opn').dialog('close');
    $("#dialog:ui-dialog").dialog("destroy");
    $('.add-opn-complete').dialog({
        modal: true,
        resizable: false,
        buttons: {
            "Ok": function () {
                $(this).dialog("close");
            }
        }, //wyczyszczenie poprzedniego okna
        title: language.saved,
        width: '400',
        height: '175'
    });
}

function updateTips(t) {
    tips
				.text(t)
				.addClass("ui-state-highlight");
    setTimeout(function () {
        tips.removeClass("ui-state-highlight", 1500);
    }, 500);
}

function checkLength(o, n, min, max, val) {
    if (!val) val = o.val();
    if (val.length > max || val.length < min) {
        o.addClass("ui-state-error");
        updateTips("Length of " + n + " must be between " +
					min + " and " + max + ".");
        return false;
    } else {
        return true;
    }
}

function checkRegexp(o, regexp, n) {
    if (!(regexp.test(o.val()))) {
        o.addClass("ui-state-error");
        updateTips(n);
        return false;
    } else {
        return true;
    }
}

function showAddOpinion(reservationNumber) {

    $('.aoc-sl').slider({
        range: 'min',
        value: 8,
        min: 1,
        max: 10,
        step: 0.1,
        slide: function (event, ui) {
            $(event.target).parent().find('.aoc-v').text(ui.value.toFixed(1));
        }
    });
    $('.aoc-v').each(function () {
        $(this).parent().find('.aoc-sl').slider('value', parseFloat($(this).text()));
    });

    $("#dialog:ui-dialog").dialog("destroy");

    var firstName = $('input[name*=FirstName]'),
            lastName = $('input[name*=LastName]'),
			country = $('input[name$=txtCountry]'),
			inplus = $('textarea[name=inplus]'),
			inminus = $('textarea[name=inminus]'),
			allFields = $([]).add(firstName).add(lastName).add(country).add(inplus).add(inminus);
    tips = $(".validateTips");

    $(".add-opn").dialog({
        autoOpen: true,
        height: 490,
        width: 800,
        resizable: false,
        modal: true,
        title: language.addOpinion + ' ' + reservationNumber,
        buttons: [
         { text: language.save,
             click: function () {
                 var bValid = true;
                 var opinion = getCurrentOpinion();

                 allFields.removeClass("ui-state-error");

                 bValid = bValid && checkLength(firstName, language.firstName, 3, 16, opinion.ClientFirstName);
                 bValid = bValid && checkLength(country, language.country, 2, 4, opinion.Country);

                 if (bValid) {
                     $.ajax({
                         type: "POST",
                         contentType: "application/json; charset=utf-8",
                         url: "/Handlers/ApartsService.asmx/AddOpinion",
                         data: JSON.stringify(opinion),
                         dataType: "json",
                         beforeSend: beforeSaveOpinion,
                         success: saveComplete
                     });
                 }
             }
         },
            {
                text: language.cancel,
                click: function () {
                    $(this).dialog("close");
                }
            }
        ],
        close: function () {
            allFields.val("").removeClass("ui-state-error");
        }
    });

}